<template>
  <div>
    <el-form :inline="true" class="demo-form-inline">
      <!--查询表单-->
      <el-form-item label="课程分类">
        <div class="block">
          <el-cascader
            v-model="value"
            :options="options"
            :props="{ checkStrictly: true, value: 'id', label: 'title' }"
            :key="modalKey"
            clearable
            @change="handleChange"
          ></el-cascader>
        </div>
      </el-form-item>
      <!-- 标题 -->
      <el-form-item>
        <el-input v-model="searchObj.title" placeholder="课程标题" />
      </el-form-item>
      <!-- 讲师 -->
      <el-form-item>
        <el-select
          row-class-name="myClassList"
          v-model="searchObj.teacherId"
          placeholder="请选择讲师"
        >
          <el-option
            v-for="teacher in teacherList"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"
          />
        </el-select>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="query()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
      row-class-name="myClassList"
    >
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column label="课程信息" width="470" align="center">
        <template slot-scope="scope">
          <div class="info">
            <div class="pic">
              <img :src="scope.row.cover" width="100px" height="150px" />
            </div>
            <div class="title">
              <strong
                ><a href="">{{ scope.row.title }}</a></strong
              >
              <p>
                总课时:<strong>{{ scope.row.lessonNum }}</strong>
              </p>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.gmtCreate.substr(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.gmtModified.substr(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="价格" width="100" align="center">
        <template slot-scope="scope">
          {{
            Number(scope.row.price) === 0
              ? "免费"
              : "¥" + scope.row.price.toFixed(2)
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="buyCount"
        label="付费学员"
        width="100"
        align="center"
      >
        <template slot-scope="scope">{{ scope.row.buyCount }}人</template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <router-link :to="'/edu/course/info/' + scope.row.id">
            <el-button type="text" size="mini" icon="el-icon-edit"
              >编辑课程信息</el-button
            >
          </router-link>
          <router-link :to="'/edu/course/chapter/' + scope.row.id">
            <el-button type="text" size="mini" icon="el-icon-edit"
              >编辑课程大纲</el-button
            >
          </router-link>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="removeCourse(scope.row.id, scope.row.cover)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import subjectApi from "@/api/edu/subject";
import teacherApi from "@/api/edu/teacher";
import courseApi from "@/api/edu/course";
export default {
  data() {
    return {
      listLoading: false,
      searchObj: {
        title: "",
        subjectId: "",
        subjectParentId: "",
      }, //搜索信息
      teacherList: [], //老师列表
      list: [], //表格数据
      //   分页数据
      page: 1,
      limit: 10,
      total: 100,
      //   级联数据
      modalKey: 1,
      value: [],
      options: [
        {
          id: "zhinan",
          title: "指南",
          children: [
            {
              id: "shejiyuanze",
              title: "设计原则",
            },
          ],
        },
      ],
    };
  },
  created() {
    this.inintData();
    this.findTeacher();
    this.findSubject();
  },
  methods: {
    // 初始化数据
    inintData() {
      courseApi
        .pageQuery(this.page, this.limit, this.searchObj)
        .then((result) => {
          this.list = result.data.rows;
          this.total = result.data.total;
        });
    },
    findTeacher() {
      teacherApi.list().then((result) => {
        this.teacherList = result.data.items;
      });
    },
    findSubject() {
      subjectApi.getNestedTreeList().then((result) => {
        this.options = result.data.items;
      });
    },
    // 分页跳转
    fetchData(page) {
      this.page = page;
      this.inintData();
    },
    // 清空搜索框
    resetData() {
      this.searchObj = {};
    },
    // 根据搜索条件查询
    query() {
      this.inintData();
    },
    //级联更改
    handleChange() {
      this.searchObj.subjectParentId = this.value[1];
      this.searchObj.subjectId = this.value[0];
    },
    cateLevel: function () {
      if (this.$refs.cascaderRef) {
        this.$refs.cascaderRef.dropDownVisible = false;
      }
    },
    //删除课程相关信息
    removeCourse(courseId) {
      courseApi.removeCourse(courseId).then(result=>{
        this.$message({
            type: "success",
            message: "删除课程成功",
          });
        this.inintData();
      });
    },
  },
};
</script>

<style>
.el-radio__inner {
  border-radius: 0;
  border: 0;
  width: 170px;
  height: 34px;
  background-color: transparent;
  cursor: pointer;
  box-sizing: border-box;
  position: absolute;
  top: -18px;
  left: -19px;
}

.el-radio__input.is-checked .el-radio__inner {
  background: transparent;
}
.myClassList .info {
  width: 450px;
  overflow: hidden;
}
.myClassList .info .pic {
  width: 150px;
  height: 150px;
  overflow: hidden;
  float: left;
}
.myClassList .info .pic a {
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
.myClassList .info .pic img {
  display: block;
  width: 100%;
}
.myClassList td .info .title {
  width: 280px;
  float: right;
  height: 90px;
}
.myClassList td .info .title a {
  display: block;
  height: 48px;
  line-height: 24px;
  overflow: hidden;
  color: #00baf2;
  margin-bottom: 12px;
}
.myClassList td .info .title p {
  line-height: 20px;
  margin-top: 5px;
  color: #818181;
}
</style>